package editor.project;

import java.io.File;

import org.jdom.Element;

/**
 * 
 * @author Mark Dessain 
 */
@SuppressWarnings("serial")
public class ProjectFile extends File
{
	
	public enum Type 
	{
		PACKAGE,
		JAVA, 
		FILE,
		FOLDER,
		SRC,
		PACKAGEEMPTY
	}
	
	// The file and parent project
	
	private boolean edited = false;
	private Package parent;
	private Project project;
	private Type type;
	
	
	/**
	 * @param fileName name of the file
	 * @param project parent
	 * @param type of file
	 * @param parent package
	 */
	public ProjectFile(String fileName, Project project, Type type, Package parent)
	{
		super(fileName);
		this.project = project;
		this.type = type;
		this.parent = parent;
	}

	/**
	 * @param fileName name of the file
	 * @param project parent
	 * @param type of file
	 */
	public ProjectFile(String fileName, Project project, Type type)
	{
		this(fileName, project, type, null);
	}
	
	/**
	 * @return the project
	 */
	public Project getProject() 
	{
		return project;
	}
	
	/**
	 * @param bool True if edited, False otherwise
	 */
	public void setEdited(boolean bool)
	{
		this.edited = bool;
	}
	
	/**
	 * @return edited status
	 */
	public boolean getEdited()
	{
		return edited;
	}

	/**
	 * @param project the project to set
	 */
	public void setProject(Project project) 
	{
		this.project = project;
	}

	/**
	 * @param type of file
	 */
	public void setType(Type type)
	{
		this.type = type;
	}

	/**
	 * @return type of file
	 */
	public Type getType()
	{
		return type;
	}
	
	/**
	 * @param parent of file
	 */
	public void setPackage(Package parent)
	{
		this.parent = parent;
	}
	
	/**
	 * @return package file belongs to
	 */
	public Package getPackage()
	{
		return parent;
	}
	
	/**
	 * @return class file location
	 */
	public String getClassFile()
	{
		return getPackage().linkPackages() +  getName().substring(0, (getName().lastIndexOf(".")));
	}
	
	/**
	 * @return an XML export
	 */
	public Element getXMLElement()
	{
		Element file = new Element("file");
		Element directory = new Element("directory");
		Element project = new Element("project");

		project.setText(this.getProject().getName());
		directory.setText(this.getAbsolutePath());
		
		
		file.addContent(directory);
		file.addContent(project);

		return file;
	}
	
	/* (non-Javadoc)
	 * @see java.io.File#toString()
	 */
	@Override
	public String toString()
	{
		if(type == Type.PACKAGE && parent == null)
		{
			return "(default package)";
		}
		else if(type == Type.PACKAGE || type == Type.PACKAGEEMPTY)
		{
			String linkedPackages = parent.linkPackages();
			return linkedPackages;//.substring(0, linkedPackages.length()-1);
		}
		else
		{
			return getName();
		}
	}
	
	/**
	 * @return location of file
	 */
	public String getFileLocation()
	{
		return super.toString();	
	}
}
